
//Eli Elder
//mine
//makefile needs this
//cardtest1: cardtest1.c dominion.o rngs.o
//	gss -o cardtest1 -g cardtest1.c dominion.o rngs.o $(CFLAGS)


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
#include <assert.h>
#include <math.h>
#include "rngs.h"

//set PRINTOUT to 0 to remove printfs out
#define PRINTOUT 1
#define ASSERTIVE 0

int checkMine(int card, int choice1, int choice2, int choice3, struct gameState *afterMineState, int handPos, int *bonus){
    
    struct gameState beforeMineState;
    int returnVal;
    int trashCard;
    int i;
    int player = whoseTurn(afterMineState);
    //copy so we can test if the mine we can check
    memcpy(&beforeMineState, afterMineState, sizeof(struct gameState));
    //do the mine function card
    returnVal = cardEffect(mine, choice1, choice2, handPos, afterMineState, handPos, 0 );

    
    trashCard = beforeMineState.hand[player][choice1];

#if (PRINTOUT == 1)
    //inform the user if choice 1 is too high or too low
    if(trashCard < copper){
	printf("Less than copper\n");
    }else if(trashCard > gold){
	printf("Greater than gold\n");
    }
    //inform if choice2 is too high or too low
    if(choice2 < curse){
	printf("less than curse, not allowedn");
    }else if(choice2 > treasure_map){
	printf("more than treasure_map, not allowed\n");
    }


    //inform user if cost is not enough (was wrong in code!)
    if((getCost(beforeMineState.hand[player][choice1]) + 3) < getCost(choice2)){
	printf("the cost of the choice1 + 3 is less than choice2 you cant get that\n");
    } 
#endif	
    
    //the before gains a card
    gainCard(choice2, &beforeMineState, 2, player);

    //we discard the handPos card
    discardCard(handPos, player, &beforeMineState, 0);
    //then we discard the card that is to be trashed
    for(i = 0; i < beforeMineState.handCount[player]; i++){
	if(beforeMineState.hand[player][i] == trashCard){
	    discardCard(i, player, &beforeMineState, 0);
	    break;
	}
    }
    
#if (PRINTOUT == 1)
    //if the full deck counts arent the same gain and discard messed up
    if(fullDeckCount(player, 0, &beforeMineState) < fullDeckCount(player, 0, afterMineState)){
	printf("The mine card has more cards than the test\n");
    } else if(fullDeckCount(player, 0, &beforeMineState) > fullDeckCount(player, 0, afterMineState)){
	printf("The mine card has less cards than the test\n");
    }
    
#endif
    
#if (ASSERTIVE == 1)
    //this will assert if something went wrong, those ifs above tell us what went wrong
    assert(returnVal == 0);
    //assert that the after MineCard state is the same as we expect it to be with the "before" that we made like the after
    assert(memcmp(&beforeMineState, afterMineState, sizeof(struct gameState)) == 0);
#endif

    return 0;
}


int main(){
    int i;
    int player = 1;
    int numDeck = 5;
    int numDiscard = 5;
    int numHand = 5;
    int initialized;
    int coppers[MAX_DECK];
    int bonus = 0;
    struct gameState theGameState;
    int kingCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, salvager};

    for(i = 0; i < MAX_DECK; i++){
	coppers[i] = copper;
    }


    //setup and initialize the state (should do in random?)                                                                                                                
    memset(&theGameState, 23, sizeof(struct gameState));
    initialized = initializeGame(player, kingCards, 1, &theGameState);

    //assign the deck, hand, and discard                                                                                                                                   
    theGameState.discardCount[player] = numDiscard;
    theGameState.handCount[player] = numHand;
    theGameState.deckCount[player] = numDeck;

    //mem set the deck hand and discard                                                                                                                                    
    memcpy(theGameState.discard[player], coppers, sizeof(int) * MAX_DECK);
    memcpy(theGameState.hand[player], coppers, sizeof(int) * MAX_HAND);
    memcpy(theGameState.deck[player], coppers, sizeof(int) * MAX_HAND);

    theGameState.whoseTurn = player;

    //check the mine
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    checkMine(mine, copper, silver, gold, &theGameState, copper, &bonus);
    printf("Mined a silver from a copper\n");

    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, silver, mine, gold, &theGameState, silver, &bonus);    
    printf("Mined a gardens from a silver\n");

    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, silver, feast, gold, &theGameState, silver, &bonus);
    printf("Mined a feast from a silver\n");
    
	printf("Note, if ASSERT and PRINTOUT are not flagged everything might look fine, but not be fine\n");
    return 0;
}
